<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="50px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="articleStatus">
        <el-select v-model="queryParams.articleStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.base_article_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="栏目" prop="channelId"  style="width:250px">
        <treeselect style="width:200px" v-model="queryParams.channelId" :options="channelOptions" :normalizer="normalizer" placeholder="请选择栏目"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['baseplatform:content:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['baseplatform:content:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['baseplatform:content:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="栏目名称" align="center" prop="channelName" />
      <el-table-column label="标题" align="center" prop="title">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isTop == 1">顶</el-tag>{{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="articleStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.base_article_status" :value="scope.row.articleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNo" />
      <el-table-column label="点赞量" align="center" prop="likes" />
      <el-table-column label="收藏量" align="center" prop="collectNum" />
      <el-table-column label="浏览量" align="center" prop="readNum" />
      <el-table-column label="评论量" align="center" prop="commentNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseplatform:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseplatform:content:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="栏目" prop="channelId">
          <treeselect v-model="form.channelId" :options="channelOptions" :normalizer="normalizer" placeholder="请选择上级栏目ID" />
        </el-form-item>
        <div style="color: red;">数值越大排序越靠前</div>
        <el-form-item label="排序" prop="orderNo">
          <el-input-number v-model="form.orderNo" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="推荐值" prop="recommentValue">
          <el-input-number v-model="form.recommentValue" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="内容简介" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容简介" />
        </el-form-item>
        <el-form-item label="内容正文">
          <editor v-model="form.articleText" :min-height="192"/>
        </el-form-item>
        <el-form-item label="首页图片" prop="imgPath">
          <image-upload v-model="form.imgPath" :limit="1"/>
        </el-form-item>
        <el-form-item label="封面图片" prop="coverImgPath">
          <span>建议尺寸:750*200或1125*600</span>
          <image-upload v-model="form.coverImgPath" :limit="1"/>
        </el-form-item>
        <el-form-item label="状态" prop="articleStatus">
          <el-radio-group v-model="form.articleStatus">
            <el-radio
              v-for="dict in dict.type.base_article_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-radio-group v-model="form.isTop">
            <el-radio
              v-for="dict in dict.type.public_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-collapse v-model="activeNames">
          <el-collapse-item title="跳转配置" name="1">
            <div style="color: red;">这里的链接类型和跳转链接是控制文章怎么跳转</div>
            <div>如果跳转第三方页面,那么选择链接类型是外部,跳转链接填写网址,如:http://www.baidu.com</div>
            <div>如果本小程序页面,选择链接类型是内部,跳转链接填写代码路径。如不填,默认所属栏目的内容代码路径,路径请咨询技术人员</div>
            <el-form-item label="链接类型" prop="linkType">
              <el-select v-model="form.linkType" placeholder="请选择活动类型" clearable>
                <el-option
                  v-for="dict in dict.type.article_link_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="跳转链接" prop="linkUrl">
              <el-input v-model="form.linkUrl" placeholder="请输入跳转链接" />
            </el-form-item>
            <el-form-item label="文章跳转的appId" prop="appId">
              <el-input v-model="form.appId" placeholder="请输入appId" />
            </el-form-item>
            <div style="color: red;">这里的链接类型和跳转链接是控制文章里的按钮怎么跳转</div>
            <el-form-item label="按钮文字" prop="showText">
              <el-input v-model="form.showText" placeholder="请输入按钮文字" />
            </el-form-item>
            <el-form-item label="链接类型" prop="linkType">
              <el-select v-model="form.butLinkType" placeholder="请选择活动类型" clearable>
                <el-option
                  v-for="dict in dict.type.article_link_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="跳转链接" prop="linkUrl">
              <el-input v-model="form.butLinkUrl" placeholder="请输入跳转链接" />
            </el-form-item>
            <el-form-item label="按钮跳转的appId" prop="butAppId">
              <el-input v-model="form.butAppId" placeholder="请输入appId" />
            </el-form-item>
          </el-collapse-item>
        </el-collapse>
        <div style="color: red;">在内容详情定位</div>
        <TxMap @selected="mapSelected" :containerId="'contentMap'"></TxMap>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>

        <el-form-item label="发布地点" prop="placeName">
          <el-input v-model="form.placeName" placeholder="请输入发布地点" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/baseplatform/content";
import { listChannel } from "@/api/baseplatform/channel";
import Treeselect from "@riophae/vue-treeselect";

import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: "Content",
  dicts: ['base_article_status', 'public_yes_no', 'article_play_type','article_link_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      activeNames: ['0'],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章内容表格数据
      contentList: [],
      //栏目树
      channelOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        subtitle: null,
        articleStatus: null,
        isQuerySub: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
            { required: true, message: '请填写标题', trigger: 'blur' }
        ],
        channelId: [
            { required: true, message: '请选择栏目', trigger: 'blur' }
        ],
        articleText:
        [
            { required: true, message: '请填写正文', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询文章内容列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        channelId: null,
        title: null,
        subtitle: null,
        orderNo: null,
        introduction: null,
        recommentValue: null,
        articleText: null,
        imgPath: null,
        coverImgPath: null,
        articleStatus: 0,
        isTop: null,
        isComment: null,
        likes: null,
        collectNum: null,
        readNum: null,
        commentNum: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        isDel: null,
        remark: null,
        linkType: null,
        linkUrl: null,
        appId: null,
        butAppId: null,
        showText: null,
        butLinkType: null,
        butLinkUrl: null,
        longitude: null,
        latitude: null,
        placeName: null,
        zoomImgPath: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      //选择了栏目，就查询栏目下的子栏目
      if(this.queryParams.channelId){
        this.queryParams.isQuerySub = '1';
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.isTop='0';
      this.form.articleStatus='0';
      // this.getTreeselect();
      this.open = true;
      this.title = "添加文章内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // this.getTreeselect();
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      //console.log(this.form.imgPath);
      if(this.form.imgPath && this.form.imgPath instanceof Array){
        this.form.zoomImgPath = this.form.imgPath[0].zoomUrl;
        this.form.imgPath = this.form.imgPath[0].url;
        //console.log("获取数组");
      }
      //内容封面图
      if(this.form.coverImgPath && this.form.coverImgPath instanceof Array){
        this.form.coverImgPath = this.form.coverImgPath[0].url;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(this.ids.length == 0){
        this.ids.push(row.id);
      }
      const ids = this.ids;
      //const ids = row.id || this.ids;
      this.$modal.confirm('是否确认将文章内容放入回收站？').then(function() {
        return delContent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
        this.ids = [];
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('baseplatform/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    },
    /** 查询文章管理-栏目下拉树结构 */
    getTreeselect() {
      listChannel().then(response => {
        this.channelOptions = [];
        const data = { id: 0, channelName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.channelOptions.push(data);
      });
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.channelName,
        children: node.children
      };
    },
    mapSelected(point) {
      this.form.latitude = point.lat;
      this.form.longitude = point.lng;
    }
  }
};
</script>
